import random
import time

import pymysql


def mydb(sql):
    """数据库
    param text:传入sql语句
    :return data:  返回一个元组
    """
    db = pymysql.connect(user='root',
                         password='123456',
                         host='localhost',
                         port=3306,
                         database='userinfo',
                         charset='utf8mb4')
    cs = db.cursor()
    cs.execute(sql)
    data = cs.fetchall()
    db.commit()
    db.close()
    return data


# data = mydb(f"SELECT * from user where uname = 'wangwu';")
# print(data)

def make_userid():
    """用户id"""
    uid = int(time.time())
    before = random.randint(11, 99)
    after = random.randint(10, 99)
    uid = f'{before}{uid}{after}'
    return uid

# uid = int(time.time() * 10000000)


def check_pwd(pwd):
    """密码长度
    :param pwd: 传入密码，检查是否符合要求
    :return: 符合要求返回True 失败返回False
    """
    if len(pwd) < 8:
        return False
    else:
        a = b = c = d = 0
        for i in pwd:
            if i.isdigit():
                a = 1
            elif i.islower():
                b = 1
            elif i.isupper():
                c = 1
            else:
                d = 1
        if a + b + c + d < 3:
            return False
        else:
            return True


def having_usr(usr):
    if 6 <= len(usr) <= 20:
        data = mydb(f"SELECT * from user where uname = '{usr}';")
        if len(data) == 0:
            return False
        else:
            return True
    else:
        return False


print("欢迎来到xxx 用户系统，请输入您的操作指令")
print('退出: 0')
print('注册: 1')
print('登录: 2')
print('修改密码: 3')
print('删除用户: 4')
while True:
    flag = input("请输入您的指令：")
    if flag == '0':
        break
    elif flag == '1':
        # 用户输入用户名
        username = input("请输入用户名:")
        if 6 <= len(username) <= 20:
            # 查询数据中的用户名
            data = mydb(f"SELECT * from user where uname = '{username}';")
            if len(data) == 0:
                # 没有该用户
                password = input('请输入密码:')
                # ctrl+p 查看有哪些参数
                if check_pwd(pwd=password):
                    # 合法 写入数据库
                    # 从封装的函数拿到uid
                    uid = make_userid()
                    mydb(f"insert into user values({uid},'{username}','{password}')")
                    print("注册成功")
                else:
                    print('密码不合法')
            else:
                print('该用户已经存在')
        else:
            print("该用户名长度不合法")
    elif flag == '2':
        username = input("请输入用户名:")
        data = mydb(f"SELECT * from user where uname = '{username}';")
        if len(data) != 0:
            password = input("请输入密码:")
            # 查询数据库中存储的密码
            data1 = mydb(f"SELECT upassword  from user where uname = '{username}';")
            while password != data1[0][0]:
                print('密码错误')
                password = input('请输入密码')
            print("登录成功")
        else:
            print("该用户不存在")
    elif flag == '3':
        username = input("请输入用户名:")
        data = mydb(f"SELECT * from user where uname = '{username}';")
        if len(data) == 0:
            print("该用户不存在")
        else:
            password = input("请输入原密码:")
            data1 = mydb(f"SELECT upassword  from user where uname = '{username}';")
            if password != data1[0][0]:
                print("密码输入不一致，无法修改密码")
            else:
                new_password = input("请输入新密码:")
                if check_pwd(pwd=new_password):
                    while new_password == password:
                        print("两次输入的密码一样，修改密码失败")
                        new_password = input("请输入新密码:")
                    mydb(f"update user set upassword = '{new_password}' where uname = '{username}';")
                    print("修改密码成功")
                else:
                    print("修改密码失败")
    elif flag == '4':
        deluser = input("请输入要删除的用户:")
        data = mydb(f"SELECT * from user where uname = '{deluser}';")
        if len(data) == 0:
            print("该用户不存在")
        else:
            password = input("请输入密码:")
            if password == data[0][2]:
                mydb(f"delete from user where uname = '{deluser}';")
                print("删除成功")
            else:
                print("删除失败")
    else:
        print("您输入的指令有误")





